package com.wfcm.controller;

import com.wfcm.annotation.IgnoreSign;
import com.wfcm.annotation.IgnoreToken;
import com.wfcm.entity.WfLuckyCodeEntity;
import com.wfcm.entity.WfLuckyEntity;
import com.wfcm.entity.WfNewMessageEntity;
import com.wfcm.service.WfLuckyCodeService;
import com.wfcm.service.WfLuckyService;
import com.wfcm.service.WfNewMessageService;
import com.wfcm.utils.PageUtils;
import com.wfcm.utils.Query;
import com.wfcm.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/lucky")
public class LuckyController {

    @Autowired
    private WfLuckyCodeService luckyCodeService;
    @Autowired
    private WfLuckyService luckyService;
    @Autowired
    private WfNewMessageService newMessageService;

    @RequestMapping("/selCode")
    @ResponseBody
    @IgnoreSign
    @IgnoreToken
    public R selCode(String page){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("page", page);
        //查询列表数据
        Query query = new Query(map);
        //总数
        int total = luckyCodeService.queryTotal(map);
        //列表
        List<WfLuckyCodeEntity> luckyCodeList = luckyCodeService.queryList(query);
        PageUtils pageUtil = new PageUtils(luckyCodeList, total, query.getLimit(), query.getPage());

        return R.ok().put("code", pageUtil);
    }

    @RequestMapping("/sel")
    @ResponseBody
    @IgnoreSign
    @IgnoreToken
    public R sel(){

        //列表
        List<WfLuckyEntity> luckyList = luckyService.queryList(500);

        return R.ok().put("luckyList",luckyList);
    }

    @RequestMapping("/sendLuckyMes")
    @ResponseBody
    @IgnoreSign
    @IgnoreToken
    public R sendLuckyMes(HttpServletRequest req){

        List<WfLuckyCodeEntity> luckyCodeList = luckyCodeService.queryUsers(null);

        for (int i = 0; i < luckyCodeList.size(); i++) {
            WfNewMessageEntity entity = new WfNewMessageEntity();
            entity.setUserId(luckyCodeList.get(i).getUserid());
            entity.setForeignId(888);
            entity.setType(4);
            entity.setStatus(0);
            entity.setTitle("抽奖结果公布！");
            entity.setContent("双十一好芳法课堂抽奖结果公布！");
            entity.setRemark("");
            entity.setUrl("https://www.baidu.com");
            entity.setRichText("【好芳法课堂】抽奖结果公布！<br/> \n" +
                    "儿童电话手表中奖码：WA-40925-DZDB（手机号183****7716）；<br/> \n" +
                    "苹果iPad中奖码：IP-87226-BIVL（手机号134****8800）；<br/> \n" +
                    "苹果iPhoneXR中奖码：XR-129948-BZAF（手机号158****7973）。<br/> \n" +
                    "恭喜以上获奖者！请于2018年11月20日前与好芳法课堂课程顾问联系。");
            newMessageService.save(entity);
        }
        return R.ok();
    }

}
